﻿using M_Process.API.Appliction.Command.ProcessTasksCommand;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ProcessTasksCommandHandler
{
    /// <summary>
    /// 更新工序任务状态命令处理器
    /// </summary>
    public class UpdateStatusCommandHandler : IRequestHandler<UpdateStatusCommand, int>
    {
        /// <summary>
        /// 工序任务仓储接口
        /// </summary>
        protected readonly IProcessTasksRepository processTasksRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processTasksRepository">工序任务仓储接口</param>
        public UpdateStatusCommandHandler(IProcessTasksRepository processTasksRepository)
        {
            this.processTasksRepository = processTasksRepository;
        }

        /// <summary>
        /// 更新工序任务状态命令处理器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(UpdateStatusCommand request, CancellationToken cancellationToken)
        {
            var res = await processTasksRepository.GetModelsAsync(request.ProcessTasksId);
            if (request.TaskStatus == "进行中")
            {
                res.TaskStatus = "进行中";
            }
            else if (request.TaskStatus == "暂停")
            {
                res.TaskStatus = "已暂停";
            }
            else if (request.TaskStatus == "完成")
            {
                res.TaskStatus = "已完成";
            }
            else if (request.TaskStatus == "关单")
            {
                res.TaskStatus = "已关单";
            }
            return await processTasksRepository.UpdateAsync(res);
        }
    }
}
